import socket
 
# 创建 Socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
# 绑定地址和端口
server_socket.bind(("localhost", 12345))
 
# 开始监听
server_socket.listen(5)  # 最大等待连接数为 5
print("服务器已启动，等待连接...")
 
while True:
    # 接收客户端连接
    client_socket, client_address = server_socket.accept()
    print(f"客户端已连接：{client_address}")
 
    # 接收数据
    data = client_socket.recv(1024).decode("utf-8")
    print(f"收到数据：{data}")
 
    # 发送响应
    client_socket.send("已收到数据".encode("utf-8"))
 
    # 关闭客户端连接
    client_socket.close()